<template>
  <div class="analysis">
    <a-row style="margin-top: 0" :gutter="[24, 24]">
      <a-col :sm="24" :md="12" :xl="6">
        <chart-card
          :loading="loading"
          title="总销售额度"
          :total="'￥' + soldNum"
        >
          <a-tooltip title="总销售额度" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <a-range-picker @change="filterByMonth">
              <template slot="renderExtraFooter">
                选择日期筛选订单
              </template>
            </a-range-picker>
          </div>
          <div slot="footer">
            <span>可按时间筛选销售量</span>
          </div>
        </chart-card>
      </a-col>

      <a-col :sm="24" :md="12" :xl="6">
        <chart-card :loading="loading" title="访问量">
          <a-tooltip title="访问量" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <!-- 小图表 -->
            <mini-area :visitData="visitData" />
          </div>
          <div slot="footer">
            7天平均访问量<span> {{ oneDay }}</span>
          </div>
        </chart-card>
      </a-col>

      <a-col :sm="24" :md="12" :xl="6">
        <chart-card
          :loading="loading"
          title="近7天订单成交数"
          :total="totalOrders"
        >
          <a-tooltip title="订单成交数" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-bar :ordersData="ordersData" />
          </div>
          <div slot="footer">
            {{ '每天成交约' }} <span>{{ totalOrders / 7 }}</span>
          </div>
        </chart-card>
      </a-col>
    </a-row>

    <a-row style="margin: 0 -12px">
      <a-col
        style="padding: 0 12px"
        :xl="12"
        :lg="24"
        :md="24"
        :sm="24"
        :xs="24"
      >
        <a-card
          :loading="loading"
          :bordered="false"
          style="margin-top: 24px"
          title="热销商品top10"
        >
          <hot-search :searchData="hotGoodList" />
        </a-card>
      </a-col>
      <a-col
        style="padding: 0 12px"
        :xl="12"
        :lg="24"
        :md="24"
        :sm="24"
        :xs="24"
      >
        <a-card
          :loading="loading"
          :bordered="false"
          style="margin-top: 24px;"
          title="热销商品占比top10"
        >
          <sales-data v-if="dataList" :sourceData="dataList" />
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import {
  getHotGoods,
  saledTotal,
  getSoldNumByClass,
} from '@/services/shopApi/dataSource';
import { getOrderCount } from '@/services/shopApi/order';
import { getVisitNum } from '@/services/user';
import ChartCard from '@/components/card/ChartCard';
import MiniArea from '@/components/chart/MiniArea';
import MiniBar from '@/components/chart/MiniBar';
import { mapState } from 'vuex';
import HotSearch from './HotSearch';
import SalesData from './SalesData';

export default {
  name: 'Analysis',
  i18n: require('./i18n'),
  data() {
    return {
      loading: true,
      hotGoodList: [],
      soldNum: 0,
      dataList: [],
      visitData: [],
      ordersData: [],
    };
  },
  created() {
    setTimeout(() => (this.loading = !this.loading), 1000);
    getHotGoods()
      .then((res) => {
        this.hotGoodList = res.result.rows;
      })
      .catch((err) => {
        this.$message.error(err);
      });
    this.getSaledTotal();
    getSoldNumByClass().then((res) => {
      this.dataList = this.filterSoldNum(res.result);
    });
    getVisitNum({ project_id: this.user.project_id }).then((res) => {
      this.visitData = res.result;
    });
    getOrderCount({ project_id: this.user.project_id }).then((res) => {
      this.ordersData = res.result;
    });
  },
  methods: {
    filterByMonth(date, dateStr) {
      this.getSaledTotal({ start: dateStr[0], end: dateStr[1] });
    },
    getSaledTotal(params) {
      saledTotal(params).then((res) => {
        if (!res.result.length) {
          this.soldNum = '暂无数据';
        } else {
          this.soldNum = res.result[0].total_price;
        }
      });
    },
    filterSoldNum(arr) {
      return arr.map((ele) => {
        return { item: ele.name, count: ele.sold_total };
      });
    },
  },
  computed: {
    ...mapState('account', ['user']),
    oneDay() {
      return this.visitData.reduce((pre, cur) => {
        return (pre + cur.num) / 7;
      }, 0);
    },
    totalOrders() {
      return this.ordersData.reduce((pre, cur) => {
        return pre + cur.num;
      }, 0);
    },
  },
  components: { SalesData, HotSearch, MiniBar, MiniArea, ChartCard },
};
</script>

<style lang="less" scoped>
.extra-wrap {
  .extra-item {
    display: inline-block;
    margin-right: 24px;
    a:not(:first-child) {
      margin-left: 24px;
    }
  }
}
@media screen and (max-width: 992px) {
  .extra-wrap .extra-item {
    display: none;
  }
}
@media screen and (max-width: 576px) {
  .extra-wrap {
    display: none;
  }
}
</style>
